//相机
/**
 * source: true  返回本地路径
 * sourceType: 选取的类别
 * num: 最多上传的图片张数
 * isOne: 不是订单所要上传的图片
*/
function uploadFile(files = [], args = {}) {
  let self = this;
  let datas = [];
  let i = 0;
  console.log('files', files)
  console.log('args', args)
  return new Promise(function (resolve, reject) {
    let upload = function (files) {
      let len = files.length - 1
      let file = files.shift();
      console.log('file', file)
      if (typeof file == 'object') {
        console.log("jinlaile" + file)
        datas.push(file);
        ++i;
        if (files.length > 0) {
          upload(files)
        } else {
          resolve(datas)
        }
        return;
      }

      console.log(file)
      wx.uploadFile({
        url: "https://api.chuanggj.com/wechat/upload_file.json",
        filePath: file,
        name: args.name || 'image',
        formData: args.type || { "type": 'headimg' },
        header: {
          'content-type': "multipart/form-data",
          'Authorization': wx.getStorageSync('wechat_service_5mutian_token')
        },
        success(res) {
          let data = JSON.parse(res.data);
          console.log('data', data)
          if (args.type.type == 'card') {
            datas.push({ remote_image_url: data.image_url });
          }
        },
        fail(err) {
        },
        complete(cc) {
          if (files.length > 0) {
            upload(files)
          } else {
            resolve(datas)
          }
        }
      })
    }
    if (files.length > 0) {
      upload(files);
    } else { resolve([]); }

  })
}

function previewImage(...args) {
  formatUrl(args).then(datas => {
    wx.previewImage({
      current: datas.src,
      urls: datas.imgs
    })
  })
}

function formatUrl(args) {
  let datas = {
    src: args[0],
    imgs: []
  }
  return new Promise(function (resolve, reject) {
    console.log(args[0])
    args[0].imgs.map((v) => {
      datas.imgs.push(v.image)
    })
    resolve(datas)
  })
}

function uploadFileList(files, args) {
  let self = this
  let data = {
    pics: [],
    all_success: false
  }
  if (args && args.type) {
    args.type['data_group'] = new Date().getTime() / 1000
  }
  return new Promise(function (resolve, reject) {
    let upload = function (files) {
      let len = files.length - 1
      let file = files.pop()
      wx.uploadFile({
        url: "https://api.chuanggj.com/wechat/upload_file.json",
        filePath: file,
        name: args.name || 'image',
        formData: args.type || { "type": 'headimg' },
        header: {
          'content-type': "multipart/form-data",
          'Authorization': wx.getStorageSync('wechat_service_5mutian_token')
        },
        success(res) {
          data.pics.unshift({ key: len, state: true })
          let res_data = JSON.parse(res.data)
          if (len == 0 && !args.isOne) {
            // 订单相关图片上传
            let order = res_data.order
            order.k = len
            data.order = order
          } else if (args.isOne == 1) {
            console.log(res_data)
            // 个人资料头像更新
            data.user = res_data.user
          } else if (args.isOne == 2) {
            data.data = res_data
          }
        },
        fail(err) {
          console.log('err', err)
          data.pics.unshift({ key: len, state: false })
          data.all_success = true
        },
        complete(cc) {
          if (files.length > 0) {
            upload(files)
          } else {
            resolve(data)
          }
        }
      })
    }
    upload(files)
  })
}



module.exports = {
  uploadFile: uploadFile,
  previewImage: previewImage,
  uploadFileList: uploadFileList
}
